
package pri.syl.xiaowu.websocket.netty.server.handler;

import  pri.syl.xiaowu.websocket.netty.server.handler.core.factory.HttpServerHandlerFactory;
import  pri.syl.xiaowu.websocket.netty.server.handler.core.factory.execution.HttpServerHandlerFactoryExecution;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.Getter;
import lombok.Setter;

/**
 * ClassName: HttpServerHandler
 * Description: netty服务Handler
 */
@Getter
@Setter
public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    private int maxFramePayloadLength;
    private int readTimeoutMilli;

    public HttpServerHandler(int maxFramePayloadLength, int readTimeoutMilli) {
        this.maxFramePayloadLength = maxFramePayloadLength;
        this.readTimeoutMilli = readTimeoutMilli;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
        HttpServerHandlerFactoryExecution.httpServerHandshake (ctx.channel (), msg, maxFramePayloadLength, readTimeoutMilli);
    }


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        HttpServerHandlerFactory.userEventTriggered (ctx.channel (), ((IdleStateEvent) evt).state ());
        super.userEventTriggered (ctx, evt);
    }

}
